package com.gongpingjialoan.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.SecondDetailFragment;
import com.gongpingjialoan.activity.car.VipSeeCarActivity;
import com.gongpingjialoan.bean.VipAppiontCarBean;
import com.gongpingjialoan.data.UserManager;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.utility.DhUtil;
import com.gongpingjialoan.utility.Utils;
import com.gongpingjialoan.view.SelectDateDialog;

import java.util.List;

/**
 * Created by Administrator on 2016/8/4.
 */
public class VipOrderSeeCarPopup extends PopupWindow implements View.OnClickListener {

    private Context mContext;

    private SecondDetailFragment mFragment;

    private View mView, main, main1;

    private EditText mEditText;

    private ImageView close;

    private TextView timeTextView;

    private View sureView, vip_popup;

    private CheckBox mCheckBox;

    private LinearLayout carsLinearLayout;

    private VipAppiontCarBean vipAppiontCarBean;

    private LayoutInflater inflater;

    private ScrollView mScrollView;

    private SelectDateDialog dateDialog;

    private String date = "";

    private TextView numTextView, vipTextView;

    private VipAppiontCarBean.DataEntity.AppointListEntity selectAppointListEntity;

    public List<VipAppiontCarBean.DataEntity.AppointListEntity> dataEntities;

    private String car_id;

    public VipOrderSeeCarPopup(Fragment fragment, VipAppiontCarBean vipAppiontCarBean, String car_id) {
        super(fragment.getActivity());

        this.vipAppiontCarBean = vipAppiontCarBean;
        mFragment = (SecondDetailFragment) fragment;

        this.mContext = fragment.getActivity();
        this.car_id = car_id;
        inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mView = inflater.inflate(R.layout.vip_order_see_car, null);
        dateDialog = new SelectDateDialog(mContext, 7, new SelectDateDialog.OnDialogCallBack() {
            @Override
            public void onDateBack(String date1, String str) {
                timeTextView.setText(str);
                date = date1;
            }
        });


        this.setContentView(mView);
        vip_popup = mView.findViewById(R.id.vip_popup);
        numTextView = (TextView) mView.findViewById(R.id.num);
        vipTextView = (TextView) mView.findViewById(R.id.vip);
        timeTextView = (TextView) mView.findViewById(R.id.see_car_time);
        timeTextView.setOnClickListener(this);
        vipTextView.setOnClickListener(this);
        mScrollView = (ScrollView) mView.findViewById(R.id.scroll);
        carsLinearLayout = (LinearLayout) mView.findViewById(R.id.cars);
        mCheckBox = (CheckBox) mView.findViewById(R.id.checkbox);
        mCheckBox.setChecked(false);
        main = mView.findViewById(R.id.main);
        main1 = mView.findViewById(R.id.main1);
        close = (ImageView) mView.findViewById(R.id.close);
        sureView = mView.findViewById(R.id.sure_button);
        mEditText = (EditText) mView.findViewById(R.id.phone);

        if (GPJApplication.getInstance().isLogin()) {
            UserManager mUserManager = new UserManager(fragment.getActivity());
            mUserManager = mUserManager.LoadUserInfo();
            mEditText.setText(mUserManager.getPhone());
        }
        initCarsView();
        close.setOnClickListener(this);
        sureView.setOnClickListener(this);
        main.setOnClickListener(this);
        main1.setOnClickListener(this);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable cd = new ColorDrawable(0x000000);
        this.setBackgroundDrawable(cd);
    }


    public void setTextNum() {
        int max = vipAppiontCarBean.getData().getMax_count();
        int size = dataEntities.size();
        mFragment.setApponitNums(size);
        if (max - size <= 0) {
            numTextView.setText("提交即可享受VIP购车服务");
        } else {
            String num = (max - size) + "辆";
            String title = "再加入" + num + "车即可享受免费VIP服务";
            numTextView.setVisibility(View.VISIBLE);
            numTextView.setText(Utils.changeColor(mContext, title, 3, 3 + num.length(), R.color.ff0));
        }
        numTextView.setVisibility(View.GONE);
    }


    public void initCarsView() {
        carsLinearLayout.removeAllViews();
        dataEntities = vipAppiontCarBean.getData().getAppoint_list();
        setTextNum();
        if (dataEntities == null || dataEntities.size() == 0) {
            return;
        }
        int size = dataEntities.size();
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mScrollView.getLayoutParams();
        if (size > 2) {
            lp.height = DhUtil.dip2px(mContext, 200);
            mScrollView.setLayoutParams(lp);
        } else {
            lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            mScrollView.setLayoutParams(lp);
        }
        for (int i = 0; i < size; i++) {
            View view = inflater.inflate(R.layout.vip_cars_item, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.pic);
            ImageView closeImageView = (ImageView) view.findViewById(R.id.close);
            TextView titleTextView = (TextView) view.findViewById(R.id.brand_name);
            TextView priceTextView = (TextView) view.findViewById(R.id.price);
            ImageView newImage = (ImageView) view.findViewById(R.id.newcar);
            View line = view.findViewById(R.id.line);
            View imageFrame = view.findViewById(R.id.image);

            Glide.with(mContext).load(dataEntities.get(i).getThumbnail()).placeholder(R.drawable.car_loading).centerCrop().crossFade().into(imageView);
            titleTextView.setText(dataEntities.get(i).getTitle());
            String price = "售价: " + dataEntities.get(i).getPrice() + "万";
            priceTextView.setText(Utils.changeColor(mContext, price, 4, price.length(), R.color.text_orange_dark));
            if (i == size - 1) {
                line.setVisibility(View.VISIBLE);
            } else {
                line.setVisibility(View.GONE);
            }

            if ((dataEntities.get(i).getCar_id() + "").equals(car_id)) {
                newImage.setVisibility(View.VISIBLE);
            } else {
                newImage.setVisibility(View.GONE);
            }
            closeImageView.setTag(dataEntities.get(i));
            closeImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectAppointListEntity = (VipAppiontCarBean.DataEntity.AppointListEntity) v.getTag();
                    mFragment.delete(selectAppointListEntity.getId());
                }
            });

            if (TextUtils.isEmpty(dataEntities.get(i).getRegion())) {
                imageFrame.setVisibility(View.GONE);
            }
            imageFrame.setTag(dataEntities.get(i).getRegion());
            imageFrame.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, VipSeeCarActivity.class);
                    intent.putExtra("address", v.getTag().toString());
                    mContext.startActivity(intent);
                }
            });
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (vip_popup.getVisibility() == View.VISIBLE) {
                        vip_popup.setVisibility(View.GONE);
                    }
                }
            });
            carsLinearLayout.addView(view);
        }
    }


    public void remove() {
        if (selectAppointListEntity != null)
            dataEntities.remove(selectAppointListEntity);
    }

    @Override
    public void onClick(View v) {
        if (v == close) {
            dismiss();
        } else if (v == sureView) {
            String num = mEditText.getText().toString();
            if (num.length() < 11) {
                Toast.makeText(mContext, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(date)) {
                Toast.makeText(mContext, "请输入看车时间", Toast.LENGTH_SHORT).show();
                return;
            }
            if (dataEntities.size() == 0) {
                Toast.makeText(mContext, "暂无预约的车, 请先预约", Toast.LENGTH_SHORT).show();
                return;
            }
            dismiss();
            mFragment.orderVipCar(num, date, mCheckBox.isChecked(), dataEntities);
        } else if (v == timeTextView) {
            dateDialog.show();
        } else if (v == vipTextView) {
            if (vip_popup.getVisibility() == View.VISIBLE)
                vip_popup.setVisibility(View.GONE);
            else
                vip_popup.setVisibility(View.VISIBLE);
        } else if (v == main) {
            if (vip_popup.getVisibility() == View.VISIBLE) {
                vip_popup.setVisibility(View.GONE);
            } else {
                dismiss();
            }
        } else if (v == main1) {
            if (vip_popup.getVisibility() == View.VISIBLE) {
                vip_popup.setVisibility(View.GONE);
            }
        }
    }
}

